import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {

  /*CostCenterGroupDefine
    获取成本中心组列表
   */
  getCostCenterGroupList (param) {
    // let param = {
    //   "page":"",
    //   "size":"",
    //   "setOfBooksId":"",
    //   "costCenterGroupCode":"",
    //   'costCenterGroupName':""
    // }
    return httpFetch.get( `${config.baseUrl}/api/cost/center/group`, param)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //新增成本中心组
  addCostCenterGroup (param) {
    // let param = {
    //   "costCenterGroupName":"成本中心组003",
    //   "costCenterGroupCode":"ccgc003",
    //   "description":"创建成本中心组测试2",
    //   "setOfBooksId":"943461293819826177"
    // }
    return httpFetch.post(`${config.baseUrl}/api/cost/center/group`, param)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //编辑成本中心组
  editCostCenterGroup (param) {
    // let param = {
    //   "id": "1028634630862979074",
    //   "costCenterGroupName": "成本中心组001",
    //   "costCenterGroupCode": "ccgc001",
    //   "description": "创建成本中心组测试1rwerwe",
    //   "setOfBooksId": "943461293819826177",
    //   "tenantId": "943460749499830273",
    //   "enabled": true
    // }
    return httpFetch.put(`${config.baseUrl}/api/cost/center/group`, param)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //获取账套列表
  getSetOfBooks () {
    return httpFetch.get(`${config.baseUrl}/api/setOfBooks/by/tenant`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //成本中心组详情
  getCostCenterGroupDetail (id, page) {
    return httpFetch.get(`${config.baseUrl}/api/cost/center/group/one?id=${id}&page=${page}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },


  //成本中心组详情 成本中心项
  getCostCenterList (id, page) {
    return httpFetch.get(`${config.baseUrl}/api/cost/center/group/assign?costCenterGroupId=${id}&page=${page}&size=10`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  /*CostCenterGroupDetail
    成本中心组详情
    新增成本中心项
 let params =
     {
        "costCenterGroupId": "1028634630862979074",
        "costCenterId": "3198",
        "costCenterItemId": "323355",
        "tenantId": "943460749499830273",
        "costCenterName": "11111",
        "costCenterItemName": "EM00",
        "costCenterItemCode": "EM00"
    }
*/
  addCostCenterList (params) {
    return httpFetch.post(`${config.baseUrl}/api/cost/center/group/assign`, params)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  /*CostCenterGroupDetail
    成本中心组详情
    新增成本中心项
    operate:"ENABLED" 启用/禁用, "DELETED" 删除
*/
  operateCostCenterList (id, operate) {
    return httpFetch.put(`${config.baseUrl}/api/cost/center/group/assign?ids=${id}&operate=${operate}`)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  /*CostCenterGroupDetail
   成本中心组详情
   新增成本中心项
   operate:"ENABLED" 启用/禁用, "DELETED" 删除
*/
  //导入成本中心组：下载模板
  downloadCostCenterItemPersonTemplate: function () {
    return httpFetch.get(config.baseUrl + '/api/cost/center/group/assign/template/download', {}, {}, {responseType: "arraybuffer"})
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //导入成本中心组
  importTemplateCostCenterPersonOID: function (params) {
    return httpFetch.post(config.baseUrl + '/api/cost/center/group/assign/import', params,{
      'Content-Type': 'application/x-www-form-urlencoded'})
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //成本中心组：值导入后根据transactionOID查看有无错误信息
  getCostCenterBatchTransactionLog: function (transactionOID) {
    return httpFetch.get(config.baseUrl + '/api/batch/transaction/logs/v2/' + transactionOID)
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //成本中心组：值导入后若有错误信息，则下载该错误信息excel
  exportCostCenterPersonBatchTransactionLog: function (transactionOID) {
    let params = {
      transactionOID:transactionOID
    };
    return httpFetch.get(`${config.baseUrl}/api/cost/center/group/assign/failed/export`,params,{},{responseType: "arraybuffer"})
      .catch(err => {
        errorMessage(err.response);
        throw err;
      });
  },

  //get关联成本中心项模态框中的成本中心的选项
  getCostCenterItems(setOfBooksId, page) {
  // let params = {
  //   page: page,
  //   setOfBooksId: setOfBooksId,
  // }
  return new Promise(function (resolve, reject) {
    //这个接口需要做成，没有传账套就查询全部
    httpFetch.get(config.baseUrl + `/api/cost/center/company?enable=true&roleType=TENANT&setOfBooksId=${setOfBooksId}&page=${page}`)
      .then(function (res) {
        resolve({
          data: res.data,
          total: Number.parseInt(res.headers['x-total-count'])
        })
      })
      .catch(function (err) {
        errorMessage(err.response);
        reject(err.response);
      })
  })
},

}
